package com.zmh.wuye.controller;

import com.alibaba.fastjson.JSON;
import com.zmh.wuye.dao.UserMapper;
import com.zmh.wuye.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.HtmlUtils;

import java.util.HashMap;

@CrossOrigin
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/login")
    public String getUserByName(@RequestBody User user){
        String username =user.getUsername();
        HashMap<String, Object> data = new HashMap<>();
        username= HtmlUtils.htmlEscape(username);
        User userByUsername = userMapper.GetUserByName(username, user.getPassword());
        if(userByUsername==null){
            data.put("flag",400);
        }else{
            data.put("logg",userByUsername);
            data.put("flag",200);
        }
        String s = JSON.toJSONString(data);
        System.out.println(s);
        return s;
    }
    @RequestMapping("/update")
    public String update(@RequestBody User user){
        HashMap<String, Object> data = new HashMap<>();
        int update = userMapper.update(user);
        if(update>=1){
            data.put("flag",1);
        }else{
            data.put("flag",2);
        }
        String s = JSON.toJSONString(data);
        return s;
    }
}
